﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using ECS.DAL;
using ECS.DAL.Entity;
using ECS.DAL.Component;
namespace ECS.Framework
{
    public class Authorization : BaseSQLModel
    {
        #region SQL string
        private const string SQL_SEARCH = "SELECT * FROM WHERE user_id = @UserId";
        private const string SQL_PROC_AUTHORIZATION = "usp_SecManGetMenu";
        #endregion
        #region private properties

        /// <summary>
        /// section authorized list
        /// </summary>
        private Hashtable _hashAuthorized = new Hashtable();
        #endregion

        #region public properties
        /// <summary>
        /// screen sections
        /// </summary>
        public enum Sections
        { 
            MainScreen,
        }
        #endregion
        #region constructor
        public Authorization()
        {
        }
        #endregion
        

        #region public methods
        /// <summary>
        /// get authorized list for user
        /// </summary>
        /// <param name="userId"></param>
        public void GetAuthorization(string userId, string appId, string device, string platform, int locale)
        {
            DataSet ds = new DataSet();
            //StringBuilder sqlBuilder = new StringBuilder();
            //sqlBuilder.Append(SQL_PROC_AUTHORIZATION);
            //sqlBuilder.Replace("@UserId", BaseSQLModel.SqlWithQoute(userId));
            using (Entities entities = new Entities())
            {
                entities.Open();
                //select command
                DbCommand cmd = entities.CreateCommand(SQL_PROC_AUTHORIZATION);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(entities.CreateParameter("@in_vchUserID", userId));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchApplicationID", appId));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchDevice", device));
                cmd.Parameters.Add(entities.CreateParameter("@in_vchPlatform", platform));
                cmd.Parameters.Add(entities.CreateParameter("@in_intLocale", locale));
                DbDataAdapter adapter = entities.CreateDataAdapter(cmd);
                adapter.Fill(ds, "Result");
                if(ds.Tables["Resullt"].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["Status"].ToString().Equals("PASS"))
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            UserAuthorization author = new UserAuthorization();
                            author.UserId = row["UserID"].ToString();
                            author.Process = row["process"].ToString();
                            author.AppName = row["parent_menu_id"].ToString();
                            author.ResourceMasterId = row["resource_master_id"].ToString();
                            author.MenuText = row["MenuText"].ToString();
                            _hashAuthorized.Add(author.ResourceMasterId, author);
                        }
                    }
                    else
                    {
                        CommonMessageBox.ShowMessageError(ds.Tables[0].Rows[0]["Message"].ToString());
                    }

                }
                else
                {
                    CommonMessageBox.ShowMessageError("invalid dataset result");
                }
            }
        }
        /// <summary>
        /// check authized matrix
        /// </summary>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public bool IsAuthorized(string menuName)
        {
            object temp = null;
            temp = _hashAuthorized[menuName];
            if (temp != null)
            { 
                return true; 
            }
            else
            { return false; }
        }
        #endregion
    }
}
